<template>
  <div class="app-container">
    <div>
      <el-form
        ref="searchFormRef"
        :inline="true"
        :model="searchForm"
        class="demo-form-inline"
      >
        <el-form-item label="标题：" prop="title">
          <el-input v-model="searchForm.title" placeholder="请输入问卷标题" />
        </el-form-item>
        <el-form-item label="描述：" prop="introduce">
          <el-input
            v-model="searchForm.introduce"
            placeholder="请输入问卷描述"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="fetchData(true)">查询</el-button>
          <el-button type="danger" @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onAdd">新增课程</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="id" width="95" prop="id" />
      <el-table-column label="课程名称" width="150" prop="title" />
<!--      <el-table-column label="描述" width="150" prop="introduce" />-->
      <el-table-column label="创建时间" prop="create_time" />
      <el-table-column label="更新时间" prop="update_time" />
      <el-table-column label="操作" prop="op">
        <template slot-scope="scope">
          <el-button
            type="primary"
            @click="handleEdit(scope.row)"
          >查看</el-button>
          <el-button
            type="danger"
            @click="handleDel(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :page-sizes="paginationConfig.pageSizes"
      :layout="paginationConfig.layout"
      :total="paginationConfig.total"
      :page="paginationConfig.page"
      :limit="paginationConfig.pageSize"
      @pagination="handleChangePage"
    />
  </div>
</template>

<script>
import { deleteUser, getUserList } from '@/api/user'
import { getQuestionnaireList } from '@/api/questionnaire'
import {getCourseList, getCurseList} from "@/api/course";

export default {
  data() {
    return {
      list: null,
      listLoading: true,
      searchForm: {
        title: '',
        introduce: ''
      },
      paginationConfig: {
        direction: 'center', // 分页位置
        layout: 'total, sizes, prev, pager, next, jumper', // 分页类型
        pageSizes: [10, 20], // 分页pages
        total: 0,
        page: 1, // 页数-默认1
        pageSize: 10 // 每页条数-默认10
      },
      curryNew: {},
      dialogNewsDetailVisible: false
    }
  },
  created() {
    this.fetchData(true)
  },
  methods: {
    fetchData(isClear = false) {
      if (isClear) {
        this.paginationConfig.page = 1
      }
      this.listLoading = true
      const { page, pageSize } = this.paginationConfig
      const obj = {
        page,
        pageSize
      }
      getCourseList({
        ...obj,
        ...this.searchForm
      }).then((response) => {
        if (response.code === 200) {
          this.list = response.data.list
          this.paginationConfig.total = response.data.total
          this.listLoading = false
        }
      })
    },
    handleChangePage({ page, pageSize }) {
      this.paginationConfig.page = page
      this.paginationConfig.pageSize = pageSize
      this.fetchData()
    },

    onReset() {
      this.$refs.searchFormRef.resetFields()
      this.fetchData()
    },
    handleEdit(e) {
      this.$router.push({
        name: 'courseDetail',
        query: {
          id: e.id,
          type: 'edit'
        }
      })
    },
    onAdd() {
      this.$router.push({
        name: 'courseDetail',
        query: {
          type: 'add'
        }
      })
    },
    async handleDel(e) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delCourseDetail({
          id: e.id
        })
        if (res.code === 200) {
          this.$message.success(res.data)
          this.fetchData()
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.dialog-content {
  width: 375px;
  height: 600px;
  overflow: hidden;
  overflow-y: scroll;
}
</style>
